package com.alm.token.ui.viewpagefragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseAdapter;
import com.alm.token.base.BaseFragment;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.activity.BillActivity;
import com.alm.token.ui.activity.BlockWalletLoginActivity;
import com.alm.token.ui.activity.CandyActivity;
import com.alm.token.ui.activity.CurrencyDetailActivity;
import com.alm.token.ui.activity.CurrencyStakingDetailActivity;
import com.alm.token.ui.activity.KuaCurrencyActivity;
import com.alm.token.ui.activity.MessageActivity;
import com.alm.token.ui.activity.MessageDetailActivity;
import com.alm.token.ui.activity.TransformActivity;
import com.alm.token.ui.activity.TurnOutActivity;
import com.alm.token.ui.activity.TurnOutBiTBActivity;
import com.alm.token.ui.activity.TurnOutBiTB_FukuanActivity;
import com.alm.token.ui.adapter.MyAdapter;
import com.alm.token.ui.data.BaseDialogBean;
import com.alm.token.ui.data.bean.CurrencyChangeData;
import com.alm.token.ui.data.bean.CurrencyChangeIndexData;
import com.alm.token.ui.data.bean.CurrencyChangeListData;
import com.alm.token.ui.data.bean.DrawIndexData;
import com.alm.token.ui.data.bean.MiningWalletTypeIndexData;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.fragmentDialog.CandyDialogFragment;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.CurrencyChangeView;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.LanguageUtils;
import com.alm.token.utils.LogUtil;
import com.alm.token.utils.SpUtil;
import com.alm.token.widget.CustomLinearLayoutManager;
import com.alm.token.widget.HomeGridLayout;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.wangxiandeng.swipecardrecyclerview.ItemRemovedListener;
import com.wangxiandeng.swipecardrecyclerview.SwipeCardLayoutManager;
import com.wangxiandeng.swipecardrecyclerview.SwipeCardRecyclerView;
import com.yzq.zxinglibrary.android.CaptureActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import baibeiwallet.event.getUserWalletEvent;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static android.app.Activity.RESULT_OK;

//import com.alm.token.ui.activity.QRCodeScannerActivity;
//import com.yzq.zxinglibrary.android.CaptureActivity;
//import com.uuzuche.lib_zxing.activity.CaptureActivity;
//import com.uuzuche.lib_zxing.activity.CodeUtils;
//import com.uuzuche.lib_zxing.activity.ZXingLibrary;

/**
 * Created by en on 2019/1/4.
 */

public class BannerOneFragment extends BaseFragment implements CurrencyChangeView<Object>, HomeGridLayout.BtnInterface {
    @BindView(R.id.tv_sysmsg)
    TextView tvSysmsg;
    @BindView(R.id.left_horn)
    TextView tvSysmsgLeftTime;
    @BindView(R.id.tv_moresysmsg)
    TextView tvMoresysmsg;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.wdgt_homegridly)
    HomeGridLayout wdgtHomegridly;
    @BindView(R.id.tv_totalMoneyKua)
    TextView tvTotalMoneyKua;
    @BindView(R.id.img_kua)
    ImageView img_kua;

    View rootView;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;
    @BindView(R.id.rly_sysmsg_body)
    RelativeLayout rlySysmsgBody;

    private SwipeCardRecyclerView mRecyclerView;
    private MyAdapter mAdapter;


    private RefreshLoadMoreManager<UserWalletData.DataBean.WalletsBean> manager;
    private List<UserWalletData.DataBean.WalletsBean> wallets;
    private List<UserWalletData.DataBean.MiningWalletBean> miningWallet;
    private List<UserWalletData.DataBean.SysMsgBean> sysMsg = new ArrayList<>();
    private List<UserWalletData.DataBean.SysMsgBean> sysMsgNew = new ArrayList<>();
    private UserWalletData.DataBean dataBean;
    private static final int QRCODE_SCANNER_REQUEST = 1100;

    public BannerOneFragment() {

    }

    public static BannerOneFragment newInstance(UserWalletData.DataBean dataBean) {
        BannerOneFragment fragment = new BannerOneFragment();
        Bundle args = new Bundle();
        args.putParcelable("dataBean", dataBean);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getLayoutId() {
        rootView = LayoutInflater.from(getContext()).inflate(R.layout.item_banner_1, null);
        return R.layout.item_banner_1;
    }

    @Override
    protected void initVariables() {
        preenter.getCurrencyChangeIndexData();
    }

    @Override
    protected void initViews(View view) {
        initManager();
        wdgtHomegridly.setBtnInterface(this);
        recyclerView.setVisibility(View.GONE);
        nodatashowBody.setVisibility(View.VISIBLE);

        mRecyclerView = (SwipeCardRecyclerView) view.findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new SwipeCardLayoutManager());
//        final List<String> list = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            list.add(String.valueOf(i));
//        }
        mAdapter = new MyAdapter(getActivity(), sysMsg);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setRemovedListener(new ItemRemovedListener() {
            @Override
            public void onRightRemoved() {
//                showToast(list.get(list.size() - 1) + " was right removed");
                if (sysMsg.size() == 1) {
                    mRecyclerView.setVisibility(View.GONE);
                    if (sysMsgNew != null && sysMsgNew.size() != 0) {
                        rlySysmsgBody.setVisibility(View.VISIBLE);
                    } else {
//                        rlySysmsgBody.setVisibility(View.GONE);
                    }
                }
            }


            @Override
            public void onLeftRemoved() {
//                showToast(list.get(list.size() - 1) + " was left removed");
                if (sysMsg.size() == 1) {
                    mRecyclerView.setVisibility(View.GONE);
                    if (sysMsgNew != null && sysMsgNew.size() != 0) {
                        rlySysmsgBody.setVisibility(View.VISIBLE);
                    } else {
//                        rlySysmsgBody.setVisibility(View.GONE);
                    }
                }
            }
        });
        mRecyclerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent1 = new Intent(mContext, MessageDetailActivity.class);
//                intent1.putExtra("intentType", 1);
//                intent1.putExtra("sysMsgBean", (UserWalletData.DataBean.SysMsgBean) mList.get(position));
//                mContext.startActivity(intent1);
                showToast("sdsdsd");
            }
        });

    }

    public String candyUrl = "";
    public String code = "0";

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == QRCODE_SCANNER_REQUEST && resultCode == RESULT_OK && data != null) {
//            String result = null;//data.getStringExtra(com.yzq.zxinglibrary.common.Constant.CODED_CONTENT);

            String result = data.getStringExtra(com.yzq.zxinglibrary.common.Constant.CODED_CONTENT);
            Log.i("TAG", "扫描结果:" + result);
            if (!TextUtils.isEmpty(result)) {
                //https://api.coom.pub/api/candy/getCandy?mycode=104
                //说明是糖果地址,需要请求接口,领取糖果   /api/candy/getCandy?mycode=104
                if (result.indexOf("https://") != -1 || result.indexOf("http://") != -1) {
                    String[] sstrs = result.split("/api/");
                    if (sstrs.length >= 2) {
                        candyUrl = sstrs[1];
                        LogUtil.e("candyUrl=" + candyUrl);
                        String[] sstr2 = candyUrl.split("\\?");
                        if (sstr2.length >= 2) {
                            String[] sstr3 = candyUrl.split("=");
                            candyUrl = sstr2[0];
                            if (sstr3.length >= 2)
                                code = sstr3[1];
                        }
                        preenter.getDrawCandyData(ConStant.HOST + "/api/" + candyUrl, code);
                    }
                } else if (result.indexOf("CollectionPayment") != -1) {//{"coinId":"2","type":"CollectionPayment","price":"1","address":"algor","coinName":"MIO"}
                    //说明是比币互转付款二维码扫码
                    Intent intent = new Intent(getContext(), TurnOutBiTB_FukuanActivity.class);
                    intent.putExtra("scanResult", result + "");
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getContext(), TurnOutActivity.class);
                    intent.putExtra("scanResult", result + "");
                    startActivity(intent);
                }

            } else {
                Toast.makeText(getActivity(), getString(R.string.profile_hint_item3), Toast.LENGTH_SHORT).show();
            }

        } else {
//            Toast.makeText(getActivity(), getString(R.string.profile_hint_item4), Toast.LENGTH_SHORT).show();
//                AppUI.success(getContext(), "解析二维码失败");
        }

//        if (requestCode == QRCODE_SCANNER_REQUEST) {
//            if (data != null) {
//                String scanResult = data.getStringExtra("scan_result");
//                // 对扫描结果进行处理
//                showToast(scanResult);
//                Intent intent = new Intent(getContext(), TurnOutActivity.class);
//                intent.putExtra("scanResult", scanResult + "");
//                startActivity(intent);
//
////                ToastUtils.showLongToast(scanResult);
//            }
//        }
    }

    private void initManager() {
        HomeWalletAdapter adapter = new HomeWalletAdapter(getActivity());
        manager = new RefreshLoadMoreManager.Builder(getActivity())
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setLayoutManager(new CustomLinearLayoutManager(getActivity()))
                .build();

//        recyclerView.setNestedScrollingEnabled(false);

    }

    @Override
    protected void loadData() {
        if ((dataBean = getArguments().getParcelable("dataBean")) != null) {
//            Toast.makeText(getContext(), "1111ValidFragment 19999999999", Toast.LENGTH_SHORT).show();

            wallets = dataBean.getWallets();
            miningWallet = dataBean.getMiningWallet();
            sysMsg = dataBean.getSysMsg();
            sysMsgNew.clear();
            sysMsgNew.addAll(sysMsg);

            tvTotalMoneyKua.setText("≈$"+CommonUtil.saveToPoint2WithNo(dataBean.getTotalMoneyKua()));
            if (LanguageUtils.getSetIndex() == 1) {
                img_kua.setImageDrawable(getResources().getDrawable(R.drawable.icon_kua_tv_zh));
            } else {
                img_kua.setImageDrawable(getResources().getDrawable(R.drawable.icon_kua_tv_en));
            }

            if (wallets != null && wallets.size() != 0) {
                for (int i = 0; i < wallets.size(); i++) {
                    wallets.get(i).setLayoutId(HomeWalletAdapter.LAYOUT_ID);
                }
                if (EmptyUtils.isNotEmpty(miningWallet)) {
                    for (UserWalletData.DataBean.MiningWalletBean miningWalletBean : miningWallet) {
                        for (UserWalletData.DataBean.WalletsBean walletsBean1 : wallets) {
                            if (walletsBean1.getName().equalsIgnoreCase(miningWalletBean.getName())) {
                                wallets.remove(walletsBean1);
                                break;
                            }
                        }
                    }
                }
                MyApp.wallets = wallets;
                for (UserWalletData.DataBean.MiningWalletBean miningWalletBean : miningWallet) {
                    UserWalletData.DataBean.WalletsBean walletsBean = new UserWalletData.DataBean.WalletsBean();
                    walletsBean.setId(miningWalletBean.getId());
                    walletsBean.setName(miningWalletBean.getName());
                    walletsBean.setEngName(" ");
                    walletsBean.setLogo(miningWalletBean.getIcon());
                    walletsBean.setPrice(miningWalletBean.getToTypePrice());
                    walletsBean.setMoney(miningWalletBean.getMoney());
                    walletsBean.setType(-1);//挖矿的类型
                    wallets.add(walletsBean);
                }

//            wallets.addAll(wallets);//多数据模拟显示
//            wallets.addAll(wallets);//多数据模拟显示
//            wallets.addAll(wallets);//多数据模拟显示

                for (int i = 0; i < wallets.size(); i++) {
                    wallets.get(i).setLayoutId(HomeWalletAdapter.LAYOUT_ID);
                }
                manager.setData(wallets);
            }
        }


        if (sysMsg != null && sysMsg.size() != 0) {
            tvSysmsg.setText(sysMsg.get(0).getTitle());
            tvSysmsgLeftTime.setText("[" + CommonUtil.getDateTimeFromMillisecond(sysMsg.get(0).getCreateTime()).split(" ")[0] + "]");
            tvSysmsg.setTag(sysMsg.get(0));
        }
        if (sysMsg != null && sysMsg.size() != 0 && !rlySysmsgBody.isShown()) {
//            mRecyclerView.setVisibility(View.VISIBLE);

            mAdapter = new MyAdapter(getActivity(), sysMsg);
            mRecyclerView.setAdapter(mAdapter);


//            tvSysmsg.setText(sysMsg.get(sysMsg.size() - 1).getTitle());
//            tvSysmsgLeftTime.setText("[" + CommonUtil.getDateTimeFromMillisecond(sysMsg.get(sysMsg.size() - 1).getCreateTime()).split(" ")[0] + "]");
//            tvSysmsg.setTag(sysMsg.get(sysMsg.size() - 1));
        } else if (sysMsg != null && sysMsg.size() != 0) {

            mRecyclerView.setVisibility(View.GONE);
            rlySysmsgBody.setVisibility(View.VISIBLE);
//            tvSysmsg.setText(sysMsg.get(sysMsg.size() - 1).getTitle());
//            tvSysmsgLeftTime.setText("[" + CommonUtil.getDateTimeFromMillisecond(sysMsg.get(sysMsg.size() - 1).getCreateTime()).split(" ")[0] + "]");
//            tvSysmsg.setTag(sysMsg.get(sysMsg.size() - 1));
        } else {
            mRecyclerView.setVisibility(View.GONE);
//            rlySysmsgBody.setVisibility(View.GONE);
        }

        if (wallets != null && wallets.size() != 0) {
            recyclerView.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);

        } else {
            recyclerView.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {

    }

    @OnClick({R.id.tv_moresysmsg, R.id.tv_sysmsg, R.id.ly_kuabi_item})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_moresysmsg:
//                showToast("更多公告click");
                Intent intent = new Intent(getContext(), MessageActivity.class);
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.tv_sysmsg:
//                showToast("公告详情");
                if (EmptyUtils.isNotEmpty(tvSysmsg.getTag()) && tvSysmsg.getTag() instanceof UserWalletData.DataBean.SysMsgBean) {
                    Intent intent1 = new Intent(getContext(), MessageDetailActivity.class);
                    intent1.putExtra("intentType", 1);
                    intent1.putExtra("sysMsgBean", (UserWalletData.DataBean.SysMsgBean) tvSysmsg.getTag());
                    startActivity(intent1);
                }
                break;
            case R.id.ly_kuabi_item:
                startActivity(KuaCurrencyActivity.class);
                break;
        }
    }

    @Override
    public void getCurrencyChangeData(CurrencyChangeData currencyChangeData) {

    }

    @Override
    public void getCurrencyChangeListData(CurrencyChangeListData currencyChangeListData) {

    }

    @Override
    public void getCurrencyChangeIndexData(CurrencyChangeIndexData currencyChangeIndexData) {
        if (currencyChangeIndexData != null && currencyChangeIndexData.getStatus() == 0) {
//            showToast(currencyChangeIndexData.getMsg());
            MyApp.setCurrencyChangeIndexDataBean(currencyChangeIndexData.getData());
        }
    }

    @Override
    public void getDrawIndexData(DrawIndexData drawIndexData) {
        if (drawIndexData.getStatus() == 0) {
            MyApp.setDrawIndexDataBean(drawIndexData.getData());

            if (MyApp.getDrawIndexDataBean() != null) {

                //扫码
                RxPermissions rxPermissions = new RxPermissions(getActivity());
                rxPermissions.request(Manifest.permission.CAMERA)
                        .subscribe(new Observer<Boolean>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                            }

                            @Override
                            public void onNext(Boolean granted) {
                                if (granted) {
                                    if (!CommonUtil.isCameraOpen()) {
//                                                    ZXingLibrary.initDisplayOpinion(getActivity().getApplicationContext());
//                                                    Intent intent = new Intent(getActivity(), QRCodeScannerActivity.class);
////                                                    Intent intent = new Intent(getActivity(), CaptureActivity.class);
//                                                    startActivityForResult(intent, QRCODE_SCANNER_REQUEST);

                                        Intent intent = new Intent(getActivity(), CaptureActivity.class);
                                        intent.putExtra(com.yzq.zxinglibrary.common.Constant.INTENT_ZXING_CONFIG, MyApp.getZxingConfig());
                                        startActivityForResult(intent, QRCODE_SCANNER_REQUEST);


                                    } else {
                                        showToast(getString(R.string.profile_hint_item1));
                                    }
                                } else {
                                    Toast.makeText(getActivity(), getString(R.string.profile_hint_item2), Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                            }

                            @Override
                            public void onComplete() {
                            }
                        });


//                            if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//                                TLPermissionUtil.requestCameraPermissionFromActivity(rootView, getActivity());
//                            } else {
//                                if (!CommonUtil.isCameraOpen()) {
//                                    Intent intent = new Intent(getContext(), QRCodeScannerActivity.class);
//                                    startActivityForResult(intent, QRCODE_SCANNER_REQUEST);
//                                } else {
//                                    showToast("该相机目前正在被另一个应用程序打开。请关闭其他应用程序，然后再试一次。");
//                                }
//                            }

            }
        } else {
            showToast(drawIndexData.getMsg());
        }
    }

    @Override
    public void getDrawCandyData(RegisterData drawCandyData) {

        // {"msg":"恭喜获得zuoqi168派发0.0796G12","status":0,"data":null,"success":false,"total":null}
        if (drawCandyData.getStatus() == 0) {
            userMsgBean = new UserWalletData.DataBean.UserMsgBean();
            userMsgBean.setTitle("糖果");
            userMsgBean.setContent(drawCandyData.getMsg());
            userMsgBean.setEnTitle("Candy");
            userMsgBean.setEnContent(drawCandyData.getMsg());
            showCodeDialog();

        } else {
            showToast(drawCandyData.getMsg());
        }

    }

    MiningWalletTypeIndexData.DataBean data1;

    @Override
    public void getMiningWalletTypeIndexData(MiningWalletTypeIndexData miningWalletTypeIndexData) {
        if (miningWalletTypeIndexData.getStatus() == 0) {
            data1 = miningWalletTypeIndexData.getData();
        }
    }

    @Override
    public void getData(Object o) {

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            getActivity().sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            getActivity().finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

    }

    @Override
    public void ShowProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserWalletEvent1(getUserWalletEvent userWalletEvent) {
//        mBaibeiWallet = WalletManager.getInstance().getWallet(sel.index);
        LogUtil.d("刷新乐活宝钱包数据1");
        if (userWalletEvent.dataBean != null && (dataBean = userWalletEvent.dataBean) != null) {
            LogUtil.d("刷新乐活宝钱包数据2");
//            Toast.makeText(getContext(), "1111ValidFragment 19999999999", Toast.LENGTH_SHORT).show();
            wallets = dataBean.getWallets();
            miningWallet = dataBean.getMiningWallet();
            sysMsg = dataBean.getSysMsg();
            sysMsgNew = sysMsg;

            tvTotalMoneyKua.setText("≈$"+CommonUtil.saveToPoint2WithNo(dataBean.getTotalMoneyKua()));
            if (LanguageUtils.getSetIndex() == 1) {
                img_kua.setImageDrawable(getResources().getDrawable(R.drawable.icon_kua_tv_zh));
            } else {
                img_kua.setImageDrawable(getResources().getDrawable(R.drawable.icon_kua_tv_en));
            }

            if (wallets != null && wallets.size() != 0) {
                LogUtil.d("刷新乐活宝钱包数据3");
                for (int i = 0; i < wallets.size(); i++) {
                    wallets.get(i).setLayoutId(HomeWalletAdapter.LAYOUT_ID);
                }
                if (EmptyUtils.isNotEmpty(miningWallet)) {
                    for (UserWalletData.DataBean.MiningWalletBean miningWalletBean : miningWallet) {
                        for (UserWalletData.DataBean.WalletsBean walletsBean1 : wallets) {
                            if (walletsBean1.getName().equalsIgnoreCase(miningWalletBean.getName())) {
                                wallets.remove(walletsBean1);
                                break;
                            }
                        }
                    }
                }
                MyApp.wallets = wallets;
                for (UserWalletData.DataBean.MiningWalletBean miningWalletBean : miningWallet) {
                    UserWalletData.DataBean.WalletsBean walletsBean = new UserWalletData.DataBean.WalletsBean();
                    walletsBean.setId(miningWalletBean.getId());
                    walletsBean.setName(miningWalletBean.getName());
                    walletsBean.setEngName(" ");
                    walletsBean.setLogo(miningWalletBean.getIcon());
                    walletsBean.setPrice(miningWalletBean.getToTypePrice());
                    walletsBean.setMoney(miningWalletBean.getMoney());
                    walletsBean.setType(-1);//挖矿的类型
                    wallets.add(walletsBean);
                }

//            wallets.addAll(wallets);//多数据模拟显示
//            wallets.addAll(wallets);//多数据模拟显示
//            wallets.addAll(wallets);//多数据模拟显示

                for (int i = 0; i < wallets.size(); i++) {
                    wallets.get(i).setLayoutId(HomeWalletAdapter.LAYOUT_ID);
                }
                manager.setData(wallets);
            }
        }

        if (sysMsg != null && sysMsg.size() != 0) {
            tvSysmsg.setText(sysMsg.get(0).getTitle());
            tvSysmsgLeftTime.setText("[" + CommonUtil.getDateTimeFromMillisecond(sysMsg.get(0).getCreateTime()).split(" ")[0] + "]");
            tvSysmsg.setTag(sysMsg.get(0));
        }
        if (sysMsg != null && sysMsg.size() != 0 && !rlySysmsgBody.isShown()) {
            mAdapter = new MyAdapter(getActivity(), sysMsg);
            mRecyclerView.setAdapter(mAdapter);

//            tvSysmsg.setText(sysMsg.get(sysMsg.size() - 1).getTitle());
//            tvSysmsgLeftTime.setText("[" + CommonUtil.getDateTimeFromMillisecond(sysMsg.get(sysMsg.size() - 1).getCreateTime()).split(" ")[0] + "]");
//            tvSysmsg.setTag(sysMsg.get(sysMsg.size() - 1));
        } else if (sysMsg != null && sysMsg.size() != 0) {

            mRecyclerView.setVisibility(View.GONE);
            rlySysmsgBody.setVisibility(View.VISIBLE);
//            tvSysmsg.setText(sysMsg.get(sysMsg.size() - 1).getTitle());
//            tvSysmsgLeftTime.setText("[" + CommonUtil.getDateTimeFromMillisecond(sysMsg.get(sysMsg.size() - 1).getCreateTime()).split(" ")[0] + "]");
//            tvSysmsg.setTag(sysMsg.get(sysMsg.size() - 1));
        } else {
            mRecyclerView.setVisibility(View.GONE);
//            rlySysmsgBody.setVisibility(View.GONE);
        }

        if (wallets != null && wallets.size() != 0) {
            recyclerView.setVisibility(View.VISIBLE);
            nodatashowBody.setVisibility(View.GONE);

        } else {
            recyclerView.setVisibility(View.GONE);
            nodatashowBody.setVisibility(View.VISIBLE);
        }

        preenter.getCurrencyChangeIndexData();

    }

    @Override
    public void onTab(View view) {
        switch (view.getId()) {
            case R.id.tab1:
//                        showToast("扫一扫");
                preenter.getDrawIndexData("");

                break;
            case R.id.tab2:
//                        showToast("提取");
                startActivity(TransformActivity.class);

                break;
            case R.id.tab3:
//                        showToast("充值");
//                startActivity(TransferQrCodeActivity.class);
                startActivity(TurnOutBiTBActivity.class);
                break;
            case R.id.tab4:
//                        showToast("交易");
//                if (EmptyUtils.isEmpty(MyApp.getUserData()) || EmptyUtils.isEmpty(MyApp.getUserData().getData()) || EmptyUtils.isEmpty(MyApp.getUserData().getData().getUser())) {
//                    EventBus.getDefault().post(new RefreshMeEvent(1));
//                    showToast(getString(R.string.common_loading));
//                    break;
//                }
//                startActivity(TurnOutBiTB_ShoukuanActivity.class);
                startActivity(CandyActivity.class);
                break;
        }
    }

    public class HomeWalletAdapter extends BaseAdapter<UserWalletData.DataBean.WalletsBean> {

        public static final int LAYOUT_ID = R.layout.item_home_wallet;

        public HomeWalletAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final UserWalletData.DataBean.WalletsBean item, int itemType) {
            boolean canshown = SpUtil.getBoolean(MyApp.context, ConStant.NEEDRESHOWN, true);

            ImageView bg = helper.getView(R.id.bg);
//            if (item.getIndex() % 2 == 0) {
            GlideApp.with(getActivity()).load(R.drawable.rectangle_black).into(bg);
//            } else {
//                GlideApp.with(getActivity()).load(R.drawable.rectangle_green).into(bg);
//            }
            GlideApp.with(getActivity())
                    .load(item.getLogo())
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                    if (EmptyUtils.isNotEmpty(resource)) {
//                        CommonUtil.changeSVGColor(getActivity(), helper.getView(R.id.img_bizhongicon), resource, R.color.black);
//                        return true;
//                    }
                            return false;
                        }
                    })
                    .error(R.drawable.icon_arctic)
                    .into((ImageView) helper.getView(R.id.img_bizhongicon));

//            GlideApp.with(getActivity()).asGif().load("https://p3.ssl.qhimgs1.com/sdr/400__/t016393d4f01fe0bf59.gif").error(R.drawable.icon_arctic)
//                    .into((ImageView) helper.getView(R.id.img_bizhongicon));

            helper.setText(R.id.name, item.getName());
            helper.setText(R.id.desc, item.getEngName());


            helper.setText(R.id.money1, canshown ? CommonUtil.saveToPoint4WithNo(item.getMoney()) + "" : "****");
            helper.setText(R.id.money2, CommonUtil.saveToPoint4WithNo(item.getPrice()) + "");
            helper.setOnClickListener(R.id.rly_currencyitem, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if (MyApp.getChannelTypeDataBean() != null) {
                    if (item.getType() == -1) {//挖矿
                        //mbtc
                        Intent intent = new Intent(getContext(), CurrencyStakingDetailActivity.class);
                        intent.putExtra("WalletsBean", item);
//                        intent.putExtra("MiningWalletTypeIndexData", data1);//MiningWalletTypeIndexData.DataBean data1
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(getContext(), CurrencyDetailActivity.class);
                        intent.putExtra("WalletsBean", item);
                        startActivity(intent);
                    }
//                    }
                }
            });
        }


    }

    UserWalletData.DataBean.UserMsgBean userMsgBean;

    /***  弹出提示公告  ***/
    private void showCodeDialog() {
        CandyDialogFragment dialogFragment = new CandyDialogFragment();
        dialogFragment.setDialogBean(new BaseDialogBean(userMsgBean));

        dialogFragment.setListener(new CandyDialogFragment.ClickListener() {
            @Override
            public void Listener(String name, int potion) {
                startActivity(BillActivity.class);
            }
        });

        dialogFragment.show(getChildFragmentManager(), "NoticeDialogFragment");
        getFragmentManager().executePendingTransactions();
    }

}
